from flask import current_app as app, render_template, request
from find import find_ctrip,find_fliggy,find_ly
from config import SearchParams_ctrip,SearchParams_fliggy,SearchParams_ly
from mergeCSV import merge_hotel_data
import pandas as pd
import os

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/search', methods=['POST'])
def search():
    destination = request.form['destination']
    detail_destination = request.form['detail_destination']
    check_in = request.form['check_in']
    check_out = request.form['check_out']
    params_ctrip=SearchParams_ctrip(checkin=check_in, checkout=check_out,destination=destination,detailDestination=detail_destination)
    params_fliggy=SearchParams_fliggy(checkin=check_in, checkout=check_out,destination=destination,detailDestination=detail_destination)
    params_ly=SearchParams_ly(checkin=check_in, checkout=check_out,destination=destination,detailDestination=detail_destination)
    find_ctrip(params_ctrip)
    find_fliggy(params_fliggy)
    find_ly(params_ly)
    merge_hotel_data()
        # 读取合并的CSV文件
    csv_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'other', 'hotels_data_merged.csv')
    hotels_df = pd.read_csv(csv_path)
    
    # 将DataFrame转换为字典列表
    hotels = hotels_df.to_dict('records')
    
    return render_template('results.html', 
                          destination=destination, 
                          detail_destination=detail_destination,
                          check_in=check_in, 
                          check_out=check_out,
                          hotels=hotels)